package myweb;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.io.Writer;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.tomcat.util.http.fileupload.IOUtils;

import com.mysql.jdbc.StringUtils;
import com.oracle.webservices.internal.api.message.ContentType;
import com.sun.xml.internal.bind.CycleRecoverable.Context;
import com.sun.xml.internal.ws.wsdl.writer.document.Service;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class EMQXServlet extends HttpServlet {
	//用来读取post过来的json的缓存区的数据长度
	private static final int BUFFER_SIZE = 1024 * 8;
	//一些emqx post过来的数据
	private String app_id,device_id,mytime,state,data;
	
	/**
	 * 不知道是什么，反正是必须的
	 */
	private static final long serialVersionUID = 1L;
	
	//用来处理get消息
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//doPost(request,response);
		//get请求用来获取数据库的数据
		
	}
 
		//用来处理post消息
	@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//获取post过来的输入流
		InputStream in=request.getInputStream();
		//创建一个缓存读取器来暂时存贮输入流里的数据
		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		
		//body是json字符串，要解析字符串，拿到对应的值插入数据库
		//读取缓存读取器里的body数据并转为字符串格式，这里的body数据为json字符串格式
		String body = read(reader);
		//从json字符串里获取json对象
		
		JSONObject J=JSONObject.fromObject(body);
		
		//通过键值对的形式，获取json里的值并赋值给变量
			app_id= J.getString("app_id");
	
			device_id=J.getString("device_id");
			mytime=J.getString("mytime");
			state=J.getString("state");
			data=J.getString("data");
			
			
		//保存到history数据库
		DBUutil.add(app_id,device_id,mytime,state,data);
		//更新current数据库
		DBUutil.update(app_id,state,device_id);
		
}
	
	public static String read(Reader reader) throws IOException
    {
        StringWriter writer = new StringWriter();
        try
        {
            write(reader, writer);
            return writer.getBuffer().toString();
        }
        finally{ writer.close(); }
    }
	
	public static long write(Reader reader, Writer writer) throws IOException
    {
        return write(reader, writer, BUFFER_SIZE);
    }
	//把缓存器的json数据写入缓存区
	public static long write(Reader reader, Writer writer, int bufferSize) throws IOException
    {
        int read;
        long total = 0;
        char[] buf = new char[BUFFER_SIZE];
        while( ( read = reader.read(buf) ) != -1 )
        {
            writer.write(buf, 0, read);
            total += read;
        }
        return total;
    }
	
}
